//
//  LFAdsTableViewCell.m
//  LocalLife
//
//  Created by 李立达 on 15/11/24.
//  Copyright © 2015年 heima11. All rights reserved.
//

#import "LFAdsTableViewCell.h"
#import "iCarousel.h"
#import "LFFocus.h"
#import <UIImageView+WebCache.h>
@interface LFAdsTableViewCell()<iCarouselDelegate,iCarouselDataSource>

@property (weak, nonatomic) IBOutlet UILabel *titleLable;
@property (weak, nonatomic) IBOutlet iCarousel *icarouselView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (nonatomic, assign) BOOL wrap;
@property (nonatomic, strong) NSMutableArray *items;
@end
@implementation LFAdsTableViewCell

- (void)awakeFromNib {
    
    [self setUp];
    self.icarouselView.type =iCarouselTypeLinear ;
     [_icarouselView reloadData];
}

-(void)setModel:(LFHomeModel *)model
{
    _model=model;
    
    _pageControl.numberOfPages=self.model.focus.list.count;
    
    [_icarouselView reloadData];
}
- (id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        //设置初始化数据
        [self setUp];
        
        //设置type
        self.icarouselView.type = iCarouselTypeLinear;
    }
    
    return self;
}
- (void)setUp
{
    //set up data
    self.wrap = YES;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(__unused UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}
#pragma mark -
#pragma mark iCarousel methods

- (NSInteger)numberOfItemsInCarousel:(__unused iCarousel *)carousel
{
    return (NSInteger)[self.model.focus.list count];
}

- (UIView *)carousel:(__unused iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    
    //create new view if no view is available for recycling
    if (view == nil)
    {
        LFFocus *focus=self.model.focus.list[index];
        view = [[UIImageView alloc] initWithFrame:self.bounds];
        [(UIImageView *)view sd_setImageWithURL:[NSURL URLWithString:focus.cover]];
        view.contentMode = UIViewContentModeScaleToFill;
    }
    
    return view;
}

- (NSInteger)numberOfPlaceholdersInCarousel:(__unused iCarousel *)carousel
{
    
    return 2;
}

- (UIView *)carousel:(__unused iCarousel *)carousel placeholderViewAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    
    if (view == nil)
    {
         LFFocus *focus=self.model.focus.list[index];
        view = [[UIImageView alloc] initWithFrame:self.bounds];
        [(UIImageView *)view sd_setImageWithURL:[NSURL URLWithString:focus.cover]];
        view.contentMode = UIViewContentModeScaleToFill;
    }
    
    return view;
}

- (CATransform3D)carousel:(__unused iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
    //implement 'flip3D' style carousel
    transform = CATransform3DRotate(transform, M_PI / 8.0f, 0.0f, 1.0f, 0.0f);
    return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * self.icarouselView.itemWidth);
}

- (CGFloat)carousel:(__unused iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    //customize carousel display
    switch (option)
    {
        case iCarouselOptionWrap:
        {
            //normally you would hard-code this to YES or NO
            return self.wrap;
        }
        case iCarouselOptionSpacing:
        {
            //add a bit of spacing between the item views
            return value*1.0f;
        }
        case iCarouselOptionFadeMax:
        {
            if (self.icarouselView.type == iCarouselTypeCustom)
            {
                //set opacity based on distance from camera
                return 0.0f;
            }
            return value;
        }
        case iCarouselOptionShowBackfaces:
        case iCarouselOptionRadius:
        case iCarouselOptionAngle:
        case iCarouselOptionArc:
        case iCarouselOptionTilt:
        case iCarouselOptionCount:
        case iCarouselOptionFadeMin:
        case iCarouselOptionFadeMinAlpha:
        case iCarouselOptionFadeRange:
        case iCarouselOptionOffsetMultiplier:
        case iCarouselOptionVisibleItems:
        {
            return value;
        }
    }
}

#pragma mark -
#pragma mark iCarousel taps

- (void)carousel:(__unused iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
//    NSNumber *item = (self.model.focus.list)[(NSUInteger)index];
}

- (void)carouselCurrentItemIndexDidChange:(__unused iCarousel *)carousel
{
    NSLog(@"Index: %@", @(self.icarouselView.currentItemIndex));
    LFFocus *focus = _model.focus.list[self.icarouselView.currentItemIndex];
    //设置标题
    _titleLable.text = focus.title;
    
    //修改page的现实
    _pageControl.currentPage = self.icarouselView.currentItemIndex;
}

@end
